<template>
  <q-page class="cc-admin row">

    <div class="col bg-white shadow-2 q-pa-md q-ma-sm">
      <q-table
        flat
        color="primary"
        class="cross_table"
        separator="cell"
        :columns="columns"
        :data="list"
        row-key="id"
        :pagination.sync="pagination"
        :visible-columns="group"
        @request="query"
        :rows-per-page-options="[10,20,50,100]"
        :loading="loading"
        selection="multiple"
        :selected.sync="selected"
      >
        <template v-slot:top="table">
          <div class="row no-wrap full-width">
            <q-input
              clearable
              outlined
              dense
              placeholder="请输入关键字搜索"
              class="on-left"
              @input="query"
              debounce="500"
              v-model="key"
            >
              <template #append>
                <q-btn flat round icon="search" color="primary" @click="query" :loading="loading">
                  <q-tooltip>搜索</q-tooltip>
                </q-btn>

              </template>
            </q-input>
            <q-space />

            <q-space />
            <div class="q-gutter-xs">
              <q-btn icon="add" no-wrap color="primary" label="新建" @click="add('article')" />
              <q-btn icon="mdi-restore" no-wrap color="primary" label="刷新" @click="refresh" />
            </div>

          </div>

        </template>
        <template #body-cell-email="props">
          <q-td key="email" :props="props">
            <q-item v-if="props.row.email==1">显示</q-item>
            <q-item v-if="props.row.email==0">隐藏</q-item>
          </q-td>
        </template>

        <template #body-cell-opt="props">
          <q-td :props="props" :auto-width="true">
            <q-btn flat round dense color="primary" icon="edit" @click="edit(props.row)">
              <q-tooltip>编辑</q-tooltip>
            </q-btn>
            <btn-del label="角色管理" @confirm="del(props.row)" />
          </q-td>
        </template>
      </q-table>
    </div>
    <q-dialog maximized flat persistent ref="dialog">
      <q-form @submit="onSubmit" class="dialog_card column"  @reset="onReset">
        <h5 class="view_title justify-between q-px-md">
          {{editType}}角色
          <q-btn  outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <div class="row q-col-gutter-x-md dialog_form q-pa-md hidden" >
            <div class="col-3">
              <h5>
                <q-icon name="star" color="red" />角色id：
              </h5>
              <div class=" q-col-gutter-x-md">
                <q-input outlined disable  v-model="form.id" type="text"   />
              </div>
            </div>
          </div>

          <div class="row q-col-gutter-x-md dialog_form q-pa-md">
            <div class="col-5">
              <h5>
                <q-icon name="star" color="red" />角色名称：
              </h5>
              <div class=" q-col-gutter-x-md">
                <q-input outlined    v-model="form.name" type="text" :rules="[requiredTest]" />
              </div>
            </div>
          </div>

          <div class="row q-col-gutter-x-md dialog_form q-pa-md">
            <div class="col-3">
              <h5>
                密码：
              </h5>
              <div class=" q-col-gutter-x-md">
                <q-input v-model="form.password" type="password"    />
              </div>
            </div>
          </div>

          <div class="row q-col-gutter-x-md dialog_form q-pa-md">
            <div class="col-3">
              <h5>
                角色邮箱：
              </h5>
              <div class=" q-col-gutter-x-md">
                <q-input v-model="form.email" type="text"    />
              </div>
            </div>
          </div>
          <div class="row q-col-gutter-x-md dialog_form q-pa-md"></div>
          <div class="row justify-center q-pa-md">
            <div class="col-md-1 text-center">
              <q-btn
                class="no-border-radius"
                unelevated
                type="submit"
                :loading="loading"
                icon="camera_enhance"
                color="primary"
                label="保 存"
                size="md" >
                <template v-slot:loading>
                  <q-spinner-hourglass class="on-left" />保存...
                </template>
              </q-btn>
            </div>

            <div class="col-md-1 text-center">
              <q-btn
                class="no-border-radius"
                unelevated
                v-close-popup
                type="button"
                outline style="color: goldenrod;"
                icon="camera_enhance"
                label="关闭"
                size="md"
              ></q-btn>
            </div>
          </div>
        </q-scroll-area>
      </q-form>
    </q-dialog>
  </q-page>
</template>

<script>
import { IndexMixin } from 'boot/mixins';
import confirm from 'components/confirm';
import { requiredTest } from 'boot/inputTest';
import { date } from 'quasar';
const today = new Date()
const { addToDate, subtractFromDate } = date
export default {
  name: 'adminrolelist',
  mixins: [IndexMixin],
  components: {
    confirm,
  },
  data() {
    return {
      columns: [
        {
          name: 'id', align: 'center', label: 'id', field: 'id',
        },
        {
          name: 'name', align: 'left', label: '角色名称', field: 'name',
        },
        {
          name: 'id_del', align: 'left', label: '是否删除', field: 'id_del',
        },

        {
          name: 'opt', align: 'center', label: '操作', field: 'id',
        },
      ],
      loading: true,
      showQuery: true,
      headers: [{ name: 'Authorization', value: localStorage.Authorization }],
      uploadUrl: `${process.env.BASE_URL}/sys/common/upload`,
      imgUrl: `${process.env.BASE_URL}/sys/common/static`,
      url: {
        list: '/adminrolelist',
        add: '/adminroleadd',
        edit: '/adminroleupdate',
        detail: 'adminroledetail',
        delete: '/adminroledelete',
        deleteBatch: '/mnt/app/deleteBatch',
        exportXlsUrl: '/mnt/app/exportXls',
        importExcelUrl: '/mnt/app/importExcel',
      },
      form: {
        id : '',
        name : '',
        rights : '',
        id_del : '',
      },
      file_list: [],
      isEdit: 0,

    };
  },
  methods: {
    requiredTest,
    addBefore() {
      this.form.catalogId = this.catalog;
      return true;
    },
    selectCatalog(n) {
      this.catalog = n;
      this.query();
    },
    getTypeLabel( b ){
      console.log(b)
      if(b == 0){
        return '隐藏'
      }else{
        return '显示'
      }

    },
    initDict() {
    },
    onSubmit (evt) {
      console.log( this.form  )
      if( this.isEdit ){
        //this.form.attachments = this.file_list
      }

      let url = ''
      if(this.isEdit == 1){
        url = this.url.edit
      }else{
        url = this.url.add
      }
      this.$axios.post(url, this.form).then((r) => {
        this.babyWord = r;
        if(r.code == 0){
          this.file_list = []
          this.$q.notify({ position: 'top', color: 'info', textColor: 'white', icon: 'cloud_done', group: false, html: true, message: '保存成功！！',});
          this.refresh()
          this.onReset()
          this.$refs.dialog.hide();
        }else{
          this.$q.notify({ position: 'top', color: 'info', textColor: 'white', icon: 'cloud_done', group: false, html: true, message: '保存成功！！'+ r.code,});

        }
      });
      //evt.target.submit()
    },
    onReset(){
      console.log('reset')
      this.form = {
        id : 0,
        role_name : '',
        rights : '',

      }
    },
    refresh(){
      this.$axios.post('/adminrolelist').then((r) => {
        this.list = r.data;
      });
    },
  },
  mounted() {
    this.initDict();
  },
};
</script>

<style lang="stylus"></style>
